<?php
/**
 * Template Name: Pitch
 */

get_header(); ?>

<div class="container">
    <h1>
        <?php the_field('pid'); ?> - <?php

        $posts = get_field('venue');

        if ($posts): ?>
            <?php foreach ($posts as $post): // variable must be called $post (IMPORTANT) ?>
                <?php setup_postdata($post); ?>
                <?php the_title(); ?>
                <?php endforeach; ?>
            <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
            <?php endif;?>
    </h1>

    <div class="row-fluid">
        <div class="span8">

            <?php $image = wp_get_attachment_image_src(get_field('image_1'), 'pitch-main');
            if (get_field('image_1')) {
                ?>
                <img style="margin-bottom:15px; border-radius:3px;" src="<?php echo $image[0]; ?>"
                     alt="<?php echo get_the_title(get_field('image_1')) ?>"/>
                <?php } else { ?>
                <img style="margin-bottom:15px; border-radius:3px;"
                     src="http://pitchwise.co.uk/wp-content/uploads/2012/11/PitchWise-Logo-Green-658x300.png"
                     alt="<?php echo get_field('pid') ?>"/>
                <?php }?>

            <div style="margin-bottom:25px;">
                <a href="#" class="btn btn-primary pull-right">Book Now &raquo;</a>

                <?php
                $posts = get_field('venue');
                if ($posts):
                    foreach ($posts as $post):
                        $venueLink = get_permalink();
                    endforeach;
                    wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly

                endif;
                ?>
                <a href="<?php echo $venueLink ?>" class="btn btn pull-right" style="margin-right:10px;">See
                    Venue &raquo;</a>
            </div>

            <h2>Pitch Information</h2>

            <table class="table table-striped table-condensed">
                <tbody>
                <tr>
                    <th>Size</th>
                    <td><?php the_field('size'); ?> </td>
                </tr>
                <tr>
                    <th>Surface</th>
                    <td><?php the_field('surface'); ?> </td>
                </tr>
                <tr>
                    <th>Width</th>
                    <td><?php the_field('width'); ?>m</td>
                </tr>
                <tr>
                    <th>Length</th>
                    <td><?php the_field('Length'); ?>m</td>
                </tr>
                <tr>
                    <th>Floodlit</th>
                    <td><?php the_field('floodlit'); ?> </td>
                </tr>
                <tr>
                    <th>Goal Size</th>
                    <td><?php the_field('goal_size'); ?> </td>
                </tr>
                <tr>
                    <th>Indoor</th>
                    <td><?php the_field('indoor'); ?> </td>
                </tr>
                <tr>
                    <th>Nets</th>
                    <td><?php the_field('nets'); ?> </td>
                </tr>
                <tr>
                    <th>Parking</th>
                    <td><?php the_field('parking'); ?> </td>
                </tr>
                </tbody>
            </table>

            <div class="card grey">

                <form class="form-horizontal">

                    <?php
                    $posts = get_field('venue');
                    if ($posts): ?>
                        <?php foreach ($posts as $post): // variable must be called $post (IMPORTANT) ?>
                            <?php setup_postdata($post); ?>
                            <h2>Venue Information</h2>

                            <?php printVenueDetail('Address', 'address') ?>
                            <?php printVenueDetail('Website', 'website') ?>
                            <?php printVenueDetail('Notes', 'notes') ?>
                            <?php printVenueDetail('Contact Number', 'primary_contact_number') ?>
                            <?php printVenueDetail('Email', 'primary_contact_email') ?>
                            <?php printVenueDetail('Primary Contact Name', 'primary_contact_name') ?>

                            <?php endforeach; ?>
                        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
                        <?php endif; ?>
                </form>
            </div>


        </div>
        <div class="span4">
            <div class="card grey">
                <h3 class="title">Location</h3>

                <div class="card-image">
                    <div id="map_canvas" style="width: 100%; height: 250px"></div>
                </div>
                <?
                $posts = get_field('venue');
                the_field('address', $posts[0]->ID);
                ?>
            </div>

            <div class="card grey">
                <h3 class="title">Available Slots </h3>

                <select id="select-slot" style="width:276px;">
                    <?
                    for ($a = 0; $a <= 12; $a++) {
                        $currentDay = "<option value='" . date("Ymd-l", strtotime("+" . $a . " days")) . "'>";
                        $currentDay .= date("l - d/m/y", strtotime("+" . $a . " days"));
                        $currentDay .= "</option>";
                        echo $currentDay;
                    }
                    ?>
                </select>

                <table id="booking-table" class="table table-striped table-condensed table-bordered">
                    <thead>
                    <tr>
                        <th>Time</th>
                        <th>Price</th>
                        <th></th>
                    </tr>
                    </thead>
                    <tbody>
                    <?
                    $pid = get_the_ID();
                    $slot_query = new WP_Query(array('post_type' => 'slots', 'showposts' => 100));
                    while ($slot_query->have_posts()) : $slot_query->the_post();
                        $posts = get_field('pitch', $slot_query->post->ID);
                        $pitchid = $posts[0]->ID;
                        $posts = get_field('venue', $pitchid);
                        $venueid = $posts[0]->ID;
                        if ($pitchid == $pid) {
                            ?>

                        <tr data-day="<? the_field('day', $slot_query->post->ID); ?>">
                            <td><? the_field('start_time', $slot_query->post->ID); ?>
                                - <? the_field('end_time', $slot_query->post->ID); ?>
                            </td>

                            <td style="white-space:nowrap;">
                                &pound;<? echo get_field('price', $slot_query->post->ID).' <strike style="color:#333;font-weight:400;">£'.get_field('price', $slot_query->post->ID) * 1.20.'</strike>';
                                ?>

                            </td>

                            <td>
                                <form style="margin:0" method="post" action="/book/">
                                    <input type="hidden" name="slotid" value="<? echo $slot_query->post->ID ?>"/>
                                    <input type="hidden" name="pitchid" value="<? echo $pitchid ?>"/>
                                    <input type="hidden" name="venueid" value="<? echo $venueid ?>"/>
                                    <input type="hidden" name="start-time" value="<? the_field('start_time', $slot_query->post->ID); ?>"/>
                                    <input type="hidden" name="end-time" value="<? the_field('end_time', $slot_query->post->ID); ?>"/>
                                    <input type="hidden" name="day" value=""/>
                                    <input type="hidden" name="date" value=""/>

                                    <button type="submit" class="btn btn-small btn-primary" style="white-space: nowrap">Book &raquo;</button>
                                </form>

                            </td>
                        </tr>

                            <?
                        }
                    endwhile;
                    ?>
                    </tbody>

                </table>

            </div>
        </div>
    </div>
</div>


<?php get_footer(); ?>

<script src="<?php echo get_template_directory_uri(); ?>/js/PITCHWISE.map.js"></script>
<script src="<?php echo get_template_directory_uri(); ?>/js/PITCHWISE.pitch.slots.js"></script>

<script>

    $(function () {

        var options = {
        };

        var geoMap = new PITCHWISE.map(options);
        var pitchSlots = new PITCHWISE.pitch.slots(options);
    });

</script>